package com.brilliance.project.webapp.action.sysmanage;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.brilliance.project.common.report.ExcelReportExportImpl;
import com.brilliance.project.common.report.GenerateJasperPrint;
import com.brilliance.project.common.report.IBaseReportExport;
import com.brilliance.project.common.tools.DateUtil;
import com.brilliance.project.service.sample.IBrt_sService;
import com.brilliance.project.webapp.action.BaseAction;
import com.brilliance.project.webapp.form.BRT_SForm;

@Scope("prototype")
@Component("branchAction")
public class BranchAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	// 存放查询的条件
	private BRT_SForm brt_sForm = new BRT_SForm();
	// 查询的结果集
	private List<BRT_SForm> queryList = new ArrayList<BRT_SForm>();
	// 输出流变量
	private InputStream excelStream;
	// 下载文件名
	private String excelFileName;
	// service
	@Autowired
	private IBrt_sService brt_sServiceImpl = null;

	/**
	 * 查询
	 * 
	 * @return
	 */
	public String query() {
		queryList = brt_sServiceImpl.query(brt_sForm);
		return SUCCESS;
	}
	
	public String execute(){
		this.request.get("excelFileName");
		return SUCCESS;
	}

	public static void main(String[] args) {
		BranchAction ba = new BranchAction();
		List<BRT_SForm> queryList = new ArrayList<BRT_SForm>();
		BRT_SForm form = new BRT_SForm();
		form.setBrtbr("aaaa");
		queryList.add(form);
		ba.setQueryList(queryList);
		ba.exportExcel();
	}

	/**
	 * 产生excel
	 * 
	 * @return
	 */
	public String exportExcel() {
		// exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,
		// outputStream);
		return "download";
	}

	public BRT_SForm getBrt_sForm() {
		return brt_sForm;
	}

	public void setBrt_sForm(BRT_SForm brt_sForm) {
		this.brt_sForm = brt_sForm;
	}

	public List<BRT_SForm> getQueryList() {
		return queryList;
	}

	public void setQueryList(List<BRT_SForm> queryList) {
		this.queryList = queryList;
	}

	public InputStream getExcelStream() {
		final String fileName = "branch" + this.getAgent().getAgent_name() + ".xls";
		try {
			// 1.设定jasper file
			final String jasperFile = ServletActionContext.getServletContext()
					.getRealPath(File.separator) + "//ireport//branch.jasper";

			// user
			// String opUser =
			// session.getParameter("opUser")==null?"":request.getParameter("opUser");
			// 設置parameter參數

			// 取得资料
			List list = brt_sServiceImpl.queryForExport(brt_sForm);
			// 4.取得 jasper print
			LOG.info("get jasper print");
			JRDataSource dataSource = new JRBeanCollectionDataSource(list);

			final Map<String, Object> parameter = new HashMap<String, Object>();
			parameter.put("opUser", "");
			parameter.put("opDate", DateUtil.getNow());

			final GenerateJasperPrint print = new GenerateJasperPrint(
					jasperFile, parameter, dataSource);
			final Map<String, String> propertyMap = new HashMap<String, String>();
			propertyMap.put("net.sf.jasperreports.awt.ignore.missing.font",
					"true");
			print.setPropertyMap(propertyMap);
			final JasperPrint jasperPrint = print.getJasperPrint();
			// 5.設定輸出格式
			LOG.info("get the excel report export");
			final IBaseReportExport export = new ExcelReportExportImpl();
			// 6.匯出報表
			LOG.info("export excel");
			excelFileName = new String("中文名称.xls".getBytes(),"iso8859-1");
			return export.exportForInputStream(jasperPrint, fileName);
		} catch (Exception jre) {
			log.error(jre.toString());
			jre.printStackTrace();
		}
		// File file = new File(fileName);
		// try {
		// excelStream = new FileInputStream(file);
		// } catch (Exception e) {
		// e.printStackTrace();
		// }
		return null;
	}

	public void setExcelStream(InputStream excelStream) {
		this.excelStream = excelStream;
	}

	public String getExcelFileName() {
		return excelFileName;
	}

	public void setExcelFileName(String excelFileName) {
		this.excelFileName = excelFileName;
	}

	public IBrt_sService getBrt_sServiceImpl() {
		return brt_sServiceImpl;
	}

	public void setBrt_sServiceImpl(IBrt_sService brt_sServiceImpl) {
		this.brt_sServiceImpl = brt_sServiceImpl;
	}

}
